const Router = require("koa-router");
const main = require("../models");
const fs = require("fs");
const path = require("path")
const router = new Router;
router.post("/doForm", async ctx => {
    console.log(ctx.request.body);
    var { name, age, sex } = ctx.request.body;
    age = Number(age);
    // var filename = ctx.request.files.logo.path;
    // var reader = fs.createReadStream(filename);
    // var uploadFile = `./static/${path.basename(filename)}`;
    // var writer = fs.createWriteStream(uploadFile);
    // reader.pipe(writer);
    // var logo = ctx.origin + "/" + path.basename(filename);
    var obj = {
        name,
        age,
        sex,
    }
    var table = await main('member');
    // var result = await table.find({ name: obj.name }).toArray();
    // if (result.length) {
    //     ctx.body = "数据库已经添加了这位同学"
    // } else {
        var result = await table.insertOne(obj);
        await ctx.redirect("/home")
    // }

})
module.exports = router